<?php
namespace Auntie\Controller;
use Think\Controller;
class BaseController extends Controller {
	
	public static $wx;//缓存微信对象
	public static $entry;
	protected function _initialize(){

		if(!strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger")){
			$this->diemsg(0,'请使用微信浏览器访问本应用！');
		}
		
		self::$entry=array('1'=>'普洗袋洗','2'=>'品牌精洗','3'=>'洗鞋','4'=>'洗涤救治','5'=>'家政服务','6'=>'奢侈品护理');
		
		$options['appid']=C('BB_APPID_auntie');
		$options['secret']=C('BB_SECRET_auntie');
		self::$wx=new \bb\Wechat($options);

		//unset($_SESSION['auntie']['auntie_shop']);
		//session_unset();
		//session_destroy();
		
		if(!$_SESSION['auntie']['openid']){
			if(I('code')){
				$d=self::$wx->getOauthAccessToken();
				$u=self::$wx->user($d['openid']);
				if($u['subscribe']){
					$openid=$_SESSION['auntie']['openid']=$u['openid'];
					$_SESSION['auntie']['wxuser']=$u;
					$_SESSION['auntie']['set']['url']=C('BB_URL');
					//=========
					$auntie_user=M('bb_auntie')->where(array('openid'=>$openid,'status'=>1))->find();
					$shop=M('bb_shop')->where(array('shopid'=>$auntie_user['shopid'],'status'=>1))->find();
					if(!$auntie_user && !$shop){
						$url="http://mp.weixin.qq.com/s?__biz=MzI2NjA2NjM0OA==&mid=400790552&idx=1&sn=772e942e99f6a1c81ddf05140294a1d2#rd";
						header("Location:".$url);
						exit;
					}else{
						//p($shop);
						$_SESSION['auntie']['auntie_shop']=$shop;
						$_SESSION['auntie']['auntie_user']=$auntie_user;
					}
				}else{
					//没有关注公众号跳转到关注页面
					$url="http://mp.weixin.qq.com/s?__biz=MzI2NjA2NjM0OA==&mid=400790552&idx=1&sn=772e942e99f6a1c81ddf05140294a1d2#rd";
					header("Location:".$url);
					exit;
				}
			}else{
				$url=$this->snsapi_base();
				header("location:".$url);
			}
		}

		//================================
		$openid=$_SESSION['auntie']['openid'];
		if(!$_SESSION['auntie']['auntie_shop'] || !$_SESSION['auntie']['auntie_user']){
			$auntie_user=M('bb_auntie')->where(array('openid'=>$openid,'status'=>1))->find();
			$shop=M('bb_shop')->where(array('shopid'=>$auntie_user['shopid'],'status'=>1))->find();
			$_SESSION['auntie']['auntie_shop']=$shop;
			$_SESSION['auntie']['auntie_user']=$auntie_user;
		}	
		//==================================
		//时时更新用户的位置信息
		$udata=M('weixin_auntie_user')->field('latitude,longitude')->where(array('openid'=>$openid))->find();
		if($udata['latitude'] && $udata['longitude']){
			$wz=$this->get_position_pc($udata);
			$_SESSION['auntie']['wz']=$wz;
		}
	}

	//静默授权URl
	public function snsapi_base(){
		$oa_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		return self::$wx->getOAuthRedirect($oa_url,'xybingbing');
	}
	//停止不动的信息通知页面处理
	public function diemsg($status,$msg){
		//成功为1，失败为0
		$status=$status?$status:'0';
		$this->assign('status',$status);
		$this->assign('msg',$msg);
		$this->display('Base_diemsg');
		die();
	}
	//百度根据坐标找位置
	private function get_position_pc($zb=''){
		if($zb){
			$url = 'http://api.map.baidu.com/geocoder?location='.$zb['latitude'].','.$zb['longitude'].'&coord_type=bd09ll&output=json&src=jt';
			$addressData = get_curl($url);
			$address = json_decode($addressData,true);
			if($address['status']=='OK'){
				$addres['address']=$address['result']['formatted_address'];
				$addres['longitude']=$address['result']['location']['lng'];
				$addres['latitude']=$address['result']['location']['lat'];
				$addres['province']=$address['result']['addressComponent']['province'];		//省
				$addres['city']=$address['result']['addressComponent']['city'];						//市
				$addres['district']=$address['result']['addressComponent']['district'];			//区
				return $addres;
			}else{
				return FALSE;
			}
		}
	}
}